﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Jzy.Core.Common.Web;
using System.Collections.Generic;
using Frame.Lib.Core.Tree.Dir;
using Frame.Lib.Enums;
using Frame.Lib.Common;
using Frame.Lib.Utilities;

public partial class Contact_Outer_DirList : PageList
{
    private DirBO _dir = new DirBO("ContactDir");
    private string strOrders;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindView();
        }
        AddAttr();

        this.InitSelectID(hidSelectIDs);
       
        strOrders = hidOrders.Value;
        hidOrders.Value = "";

        JSUtil.SetTableHeight("list", 115);
    }
    private void AddAttr()
    {
        JSUtil.DelConfirm(btnDelete, "目录");
        JSUtil.MoveTree(btnMove, "ContactDir", hidSelectIDs, btnRefresh);
        JSUtil.ClickBtn(lnkDelete, btnDelete);
        JSUtil.ClickBtn(lnkMove, btnMove);
        JSUtil.ClickBtn(lnkSaveOrders, btnSaveOrders);
    }
    private void BindView()
    {
        LVUtil.Init(lv, lvHeader);
        List<DirInfo> list = _dir.GetTreeList();

        lv.DataSource = list; 
        lv.DataKeyNames = new string[] { "ID", "UpperID" };
        lv.DataBind();


        if (IsPostBack)
        {
            LVUtil.RestoreTreeListState(lv, hidHideTrIDs.Value);
        }

    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        BindView();
    }   
    protected void lv_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            LVUtil.SetEdit(sender, e, 320, 161, GlobalVar.WebCurrPath + "DirEdit.aspx");
            HtmlTableRow row = null;
            foreach(Control ctl in e.Item.Controls)
            {
                if (ctl is HtmlTableRow)
                {
                    row = (HtmlTableRow)ctl;
                    break;
                }
            }          
            string strID = lv.DataKeys[((ListViewDataItem)e.Item).DataItemIndex]["ID"].ToString();
            row.Attributes["id"] = strID;
            string upperID = lv.DataKeys[((ListViewDataItem)e.Item).DataItemIndex]["UpperID"].ToString();
            row.Attributes.Add("UpperID", upperID);

            CheckBox chkSelect = (CheckBox)e.Item.FindControl("chkSelect");
            if (upperID == Constants.TREE_ROOT_UPPER_ID.ToString())
            {
                chkSelect.Enabled = false;
            }

            string strUrl = GlobalVar.WebCurrPath + "DirEdit.aspx?UpperID=" + StrUtil.EncryptUrl(strID);
            HyperLink lnkAdd = (HyperLink)e.Item.FindControl("hypAdd");
            JSUtil.ShowForm(lnkAdd, "增加目录", null, strUrl, 320, 161);
        }
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (strSelectIDs == "")
        {
            return;
        }

        List<int> ids = StrUtil.SplitToInt(strSelectIDs);

        _dir.Delete(ids.ToArray());
        BindView();
    }

    protected void btnSaveOrders_Click(object sender, EventArgs e)
    {
        List<int> ids = new List<int>();
        List<int> orders = new List<int>();
        StrUtil.SplitIDOrders(ids, orders, strOrders);
        _dir.SaveOrders(ids.ToArray(), orders.ToArray());
        BindView();
    }
    
}
